﻿using System;
using System.Collections.Generic;
#if DEBUG
using System.Diagnostics;
#endif // #if DEBUG
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using FRL.DebtChased.Extensions;
using FRL.DebtChased.Repository;
using FRL.DebtChased.WorkflowObjects;
using FRL.DebtChased.Silverlight.Library;

namespace FRL.DebtChased.WorkflowEditor.WorkflowItemEditors
{

	/// <summary>
	/// Declaration of SendEmailCtrl class
	/// Controls for editing the workflow action to send an email to the debtor, client, agent or solicitor
	/// </summary>
	public partial class SendEmailCtrl : ActionBaseCtrl
	{

		/////////////////////////////////////////////////////////////
		// Construction

		public SendEmailCtrl()
		{

			// Initialize resources
			InitializeComponent();

		}

		/////////////////////////////////////////////////////////////
		// Implementation

		public override List<string> Validate()
		{

			// Declare return variable
			List<string> Ftmp = null;

			// Must have a comm-body
			if (ctrlCommBody.IsEmpty)
				Extensions.Extensions.SafeAddString(ref Ftmp, "Select an email body.");

			// Return variable
			return Ftmp;

		}

		public override void UpdateControls(WorkflowAction_Base action)
		{

			// Block update events
			this.BlockUpdateEvents = true;
			{

				// Cast the action
				WorkflowAction_SendEmail sendEmailAction = action as WorkflowAction_SendEmail;
#if DEBUG
				Debug.Assert(sendEmailAction != null);
#endif // #if DEBUG

				// Set controls - comm body
				ctrlCommBody.Set(sendEmailAction.CommBody);

				// Set controls - target
				ComboBoxObject.SelectByItemData(cboTarget, (int)sendEmailAction.Target);

			}
			this.BlockUpdateEvents = false;

			// Setup controls
			_setupControls();

		}

		public override void UpdateFromControls(ref WorkflowAction_Base action)
		{

			// If the action needs changing...
			if (!(action is WorkflowAction_SendEmail))
				action = new WorkflowAction_SendEmail();

			// Cast the action
			WorkflowAction_SendEmail sendEmailAction = action as WorkflowAction_SendEmail;
#if DEBUG
			Debug.Assert(sendEmailAction != null);
#endif // #if DEBUG

			// Update object - comm body
			ctrlCommBody.Get(sendEmailAction.CommBody);

			// Update object - target
			sendEmailAction.Target = (Definitions.eCommTargetType) (cboTarget.SelectedItem as ComboBoxObject).ItemData;

			// Update object - attachment
			sendEmailAction.AttachmentLetterTypeId = (cboAttachment.SelectedValue as ComboBoxObject).ItemData;

		}

		public override void InitializeControls()
		{

			// Comm body
			ctrlCommBody.InitializeControls(CachedData.Singleton.CommBodies);

			// Set controls - Attachment
			ComboBoxObject.LoadFromDictionary(cboAttachment,CachedData.Singleton.LetterTypeDictionary);
			cboAttachment.SelectedIndex = 0;

			// Set controls - target
			TransportSupport.LoadCommTargetTypeControl(cboTarget, Definitions.eCommTargetType.Debtor);
			ComboBoxObject.SelectByItemData(cboTarget, (int)Definitions.eCommTargetType.Debtor);

			// unblock update events
#if DEBUG
			Debug.Assert(this.BlockUpdateEvents);
#endif // #if DEBUG
			this.BlockUpdateEvents = false;

			// setup controls
			_setupControls();

		}

		/////////////////////////////////////////////////////////////
		// Event handlers

		private new void ActionBaseCtrl_Loaded(object sender, RoutedEventArgs e)
		{

			// Call base-class function
			base.ActionBaseCtrl_Loaded(sender,e);

			// setup controls
			_setupControls();

		}

		private void cboTarget_SelectionChanged(object sender, SelectionChangedEventArgs e) { _controlChange(); }
		private void ctrlCommBody_SelectionChanged(object sender, EventArgs e) { _controlChange(); }
		private void cboAttachement_SelectionChanged(object sender, SelectionChangedEventArgs e) { _controlChange(); }

		/////////////////////////////////////////////////////////////
		// Operations

		private void _controlChange()
		{

			// Shortcuts
			if (this.BlockUpdateEvents)
				return;

			// If this control is in a tabbed panel...
			if (m_pnlAddAction != null)
				m_pnlAddAction.UpdateFromCtrl(this);

		}

		private void _setupControls()
		{

		}

	}

}
